package demo;

import java.io.IOException;
import java.net.InetAddress;
import jpcap.*;
import jpcap.packet.*;

public class TestJpcap {
	public static void main(String[] args) throws IOException {
// SendPacket();
		 Demo();
//		OneByOneReceiver();
//		// CallBackReceiver();
//		System.exit(0);
	}

	// ��ȡ����ӿ��б�
	public static void Demo() {
		
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

		for (int i = 0; i < devices.length; i++) {
			System.out.println(i + ": " + devices[i].name + "("	+ devices[i].description + ")");
			System.out.println(" datalink: " + devices[i].datalink_name + "("	+ devices[i].datalink_description + ")");
			
			System.out.print(" MAC address:");
			for (byte b : devices[i].mac_address)
				System.out.print(Integer.toHexString(b & 0xff) + ":");
			System.out.println();
			
			//��ȡIP������Ϣ
			
			
			for (NetworkInterfaceAddress a : devices[i].addresses){
				System.out.println(" address:" + a.address + " " + a.subnet
						+ " " + a.broadcast);
			}
		}
	}

	// ʹ�ûص�����, ������ӿڲ�����ݰ�
	public static void CallBackReceiver() throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		int index = 2;
		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		while (true) {
			captor.processPacket(100, new PacketPrinter());
		}

		// captor.close();

	}

	// ʹ��������񷽷�, ������ӿڲ�����ݰ�
	public static void OneByOneReceiver() throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		int index = 2;
		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		// ���ù�����
		captor.setFilter("ip and tcp", true);
		jpcap.packet.IPPacket ipPacket;
		TCPPacket tcpPacket;
		Packet packet;
		while (true) {
			packet = captor.getPacket();
			if (packet != null) {
				ipPacket = (IPPacket) packet;
				System.out.println("length :" + ipPacket.length);
				System.out.println("src_ip :" + ipPacket.src_ip);
				System.out.println("dst_ip :" + ipPacket.dst_ip);
				
				tcpPacket = (TCPPacket) ipPacket;
				 System.out.println("des_port :" + tcpPacket.dst_port);
			}
		}
		
//		captor.close();

	}

	// ��д���ļ��ķ�����ȡ�������Ϣ
	public static void PacketWriteFile() throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		int index = 2;
		JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
				false, 20);
		JpcapWriter writer = JpcapWriter.openDumpFile(captor, "yanrong");
		for (int i = 0; i < 10; i++)
			writer.writePacket(captor.getPacket());
		writer.close();
	}

	//ֱ������ʾ��IO���д�ӡ��ݰ�
	public static void PacketReadFile() throws IOException {
		JpcapCaptor captor = JpcapCaptor.openFile("yanrong");
		Packet packet;
		while (true) {
			packet = captor.getPacket();
			if (packet == null) // || packet == Packet.EOF)
				break;
			System.out.println(packet.toString());
		}
	} // ͨ������ӿڷ�����ݰ�

	public static void SendPacket() throws IOException {
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		JpcapSender sender = JpcapSender.openDevice(devices[1]);
		TCPPacket p = new TCPPacket(12, 23, 56, 78, false, false, false, false,
				true, true, true, true, 10, 10);
		p.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0,
				1010101, 100, IPPacket.IPPROTO_TCP, InetAddress
						.getByName("www.baidu.com"), InetAddress
						.getByName("localhost"));
		p.data = ("data").getBytes();
		EthernetPacket ether = new EthernetPacket();
		ether.frametype = EthernetPacket.ETHERTYPE_IP;
		ether.src_mac = new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3,
				(byte) 4, (byte) 5 };
		ether.dst_mac = new byte[] { (byte) 0, (byte) 6, (byte) 7, (byte) 8,
				(byte) 9, (byte) 10 };
		p.datalink = ether;
		sender.sendPacket(p);
		sender.close();
		System.out.println(p);
	}
}

/** ʹ�ûص������������ȡ��ݰ�ʱ�õ� */
class PacketPrinter implements PacketReceiver {
	@Override
	public void receivePacket(Packet packet) {
		// System.out.println(packet.toString());
		System.out.println("cap_len = " + packet.caplen);
		System.out.println("len = " + packet.len);

	}
}
